import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/page/home/convenient/notice/notice_page_controller.dart';
import 'package:kkhousekeep/widget/notice_item.dart';

import '../../../../widget/custom_app_bar.dart';

class NoticePage extends StatelessWidget {
  const NoticePage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<NoticePageController>(
      init: NoticePageController(),
      builder: (controller) => Scaffold(
        appBar: CustomAppBar(
          "通知公告",
          actions: [
            _buildAppBarMakeReadBtn(controller),
          ],
        ),
        body: Container(
          width: double.infinity,
          color: const Color(0xFFf4f5f6),
          child: SafeArea(
            left: false,
            right: false,
            child: Obx(
              () => Column(
                children: [
                  _buildList(controller),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildAppBarMakeReadBtn(NoticePageController controller) {
    return TextButton(
        onPressed: () => controller.allRead(),
        child: Text(
          "一键已读",
          style: TextStyle(color: const Color(0xFF6D6D6D), fontSize: 32.0.sp),
        ));
  }

  Widget _buildList(NoticePageController controller) {
    return controller.noticeList.isNotEmpty
        ? Expanded(
            child: Padding(
              padding: EdgeInsets.only(top: 24.h),
              child: ListView.separated(
                  clipBehavior: Clip.none,
                  itemBuilder: (context, index) {
                    return _buildNoticeItem(controller, index);
                  },
                  separatorBuilder: (context, index) => Divider(
                        height: 16.h,
                        thickness: 0,
                        indent: 0,
                        color: Colors.transparent,
                      ),
                  itemCount: controller.noticeList.length),
            ),
          )
        : Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset(
                  "assets/images/icon_message_empty.png",
                  width: 288.w,
                  height: 358.w,
                ),
                Text(
                  "还没有通知哦",
                  style: TextStyle(
                    color: const Color(0xFF999999),
                    fontSize: 28.sp,
                  ),
                ),
              ],
            ),
          );
  }

  Widget? _buildNoticeItem(NoticePageController controller, int index) {
    return NoticeItem(controller.noticeList[index],
        () => controller.buildNoticeItem(controller.noticeList[index]));
  }
}
